var xhr;
var xhr2;
/*****************************************************************************/
/* search */
function searchEmployees() {

    //create xhr
    if (window.ActiveXObject)
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        xhr = new XMLHttpRequest;

    //xhr.open("POST", "SearchCustomersServlet?timeStamp=" + new Date().getTime(), true);
    xhr.onreadystatechange = handleSearchResults;
    xhr.open("GET", "xml/employees.xml", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlendoded");
    xhr.send(null);
}
/*---------------------------------------------------------------------------*/
function handleSearchResults() {

    if (xhr.readyState != 4) return;
    if (xhr.status != 200) {
        alert(xhr.status); // for troubleshooting
        return;
    }
    /* get the response */
    var xhresponse = xhr.responseXML;
    var nodEmployees = xhresponse.getElementsByTagName("Employee");
    var nodEmployee;

    /* get references first*/
    var thd = document.getElementById("thdResults");
    var tbd = document.getElementById("tbdResults");
    var btnDelete = document.getElementById("btnDelete");
    
    /* clear revious table*/
    removeChildren(thd);
    removeChildren(tbd);
    if (btnDelete != null)
        document.getElementById("fsetSearch").removeChild(btnDelete);
        
    /* build table header */
    if (nodEmployees.length > 0)
        insertHeaderRow(document.getElementById("thdResults"));

    /* build table body */
    for (i=0; i < nodEmployees.length; i++) {
        
        nodEmployee = nodEmployees[i]
        strID = nodEmployee.getElementsByTagName("EmployeeID")[0].firstChild.nodeValue;
        strFirstName = nodEmployee.getElementsByTagName("FirstName")[0].firstChild.nodeValue;
        strLastName = nodEmployee.getElementsByTagName("LastName")[0].firstChild.nodeValue;
        strEmail = nodEmployee.getElementsByTagName("Email")[0].firstChild.nodeValue;
        strContact = nodEmployee.getElementsByTagName("Contact")[0].firstChild.nodeValue;
            
        insertRow(tbd, strID, strFirstName + " " + strLastName, strContact, strEmail);
    } 
    /* build delete button */
    if (nodEmployees.length > 0) {
        
        btnDelete = document.createElement("input");
        btnDelete.setAttribute("id", "btnDelete");
        btnDelete.setAttribute("value", "Delete");
        btnDelete.setAttribute("type", "button");
        btnDelete.setAttribute("class", "ipInline");
        btnDelete.onclick = function() { deleteEmployee(); } 
        document.getElementById("fsetSearch").appendChild(btnDelete);
    }
}
/*****************************************************************************/
/* delete employee */
function deleteEmployee(){
    
    /* create xhr */
    if (window.ActiveXObject)
        xhr2 = new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest)
        xhr2 = new XMLHttpRequest;

    /* put all ids in colon-delimited string */
    var strDeletes = "";
    
    /* get all checkboxes */
    var chkDeletes = document.getElementsByTagName("input");
    for (i = 0; i < chkDeletes.length; i++) {
        
        if (chkDeletes.item(i).getAttribute("type") != "checkbox") continue;

        if (chkDeletes[i].checked == false) continue;

        strDeletes += chkDeletes[i].getAttribute("id") + ";";
    }
    /* nothing to delete? */
    if (strDeletes == "") return;
     
    /* prepare xhr */
    xhr2.onreadystatechange = handleDeleteResults;
    xhr2.open("POST", "EmployeesServlet?timeStamp=" + new Date().getTime(), true);
    xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlendoded");
    xhr2.send("action=delete&ids=" + strDeletes);
}
/*---------------------------------------------------------------------------*/
function handleDeleteResults() {

    var xhresponse = xhr2.responseXML;
    var strResults = xhresponse.getElementsByTagName("ID");
    var tbd = document.getElementById("tbdResults");
    
    var iDeleted;
    for (i = 0; i < strResults.length; i++) {
        iDeleted = parseInt(strResults[i]);

        if (iDeleted > 0) deleteRow(tbd, "tr" + iDeleted);
    }
}
/*****************************************************************************/
/*helpers*/
function insertHeaderRow(thd) {

    var tr = document.createElement("tr");
  
    insertCell(tr, "");     // delete checkbox
    insertCell(tr, "");     // edit button
    insertCell(tr, "ID");
    insertCell(tr, "Full Name");
    insertCell(tr, "Contact");
    insertCell(tr, "Email");

    thd.appendChild(tr);
}
/*---------------------------------------------------------------------------*/
function insertRow(tbd, id, fname, contact, email) {
    
    /* create new row */
    var tr = document.createElement("tr");
    tr.setAttribute("id", "tr" + id); // for identification later
    
    /* insert cells to new row */
    insertCheckBox(tr, "chkDelete" + id);
    insertEditButton(tr, "btnEdit" + id);
    
    insertCell(tr, id);
    insertCell(tr, fname);
    insertCell(tr, contact);
    insertCell(tr, email);
    
    /* add row to table body */
    tbd.appendChild(tr);
}
/*---------------------------------------------------------------------------*/
function deleteRow(tbd, id) {

    var trDelete = document.getElementById(id);
    if (trDelete != null) tbd.removeChild(trDelete);
}
/*---------------------------------------------------------------------------*/
function insertCell(tr, value) {

    var td = document.createElement("td");
    var txtn = document.createTextNode(value);
 
    td.appendChild(txtn);
    tr.appendChild(td);
}
/*---------------------------------------------------------------------------*/
function insertCheckBox(tr, id) {

    var td = document.createElement("td");
    var chk = document.createElement("input");
    chk.setAttribute("type", "checkbox");
    chk.setAttribute("id", id);
    td.appendChild(chk);
    tr.appendChild(td);
}
/*---------------------------------------------------------------------------*/
function insertEditButton(tr, id) {

    var td = document.createElement("td");
    var btn = document.createElement("input");
    btn.setAttribute("id", id);
    btn.setAttribute("type", "button");
    btn.setAttribute("value", "Edit");

    td.appendChild(btn);
    tr.appendChild(td);
}
/*****************************************************************************/